/**
 * Project Name:dynamicDomain
 * File Name:DynamicDomainController.java
 * Package Name:com.helper.domain.controller
 * Date:2021年4月16日上午11:13:03
 * Copyright (c) 2021, hejiahua@szhuarong.com All Rights Reserved.
 *
*/

package com.helper.domain.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.helper.domain.config.DnsPodProperties;
import com.helper.domain.exception.CustomException;
import com.helper.domain.service.DynamicDomainService;
import com.helper.domain.task.AutoRefrshDnsRecordTask;
import com.helper.domain.utils.IPUtil.QueryIPType;

import cn.hutool.core.util.ArrayUtil;

/**
 * ClassName:DynamicDomainController <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON. <br/>
 * Date: 2021年4月16日 上午11:13:03 <br/>
 * 
 * @author hejiahua
 * @version
 * @see
 */
@Controller
public class DynamicDomainController {

	@Autowired
	DynamicDomainService dynamicDomainService;
	
	@GetMapping("/")
	public String index() {
		return "index.html";
	}
	
	@GetMapping("/ping")
	public String ping() {
		return "index.html";
	}
	
	@GetMapping("/pingip")
	@ResponseBody
	public String pingip() {
		return DynamicDomainService.cacheIP;
	}
	
	@Autowired
	DnsPodProperties dnsPodProperties;
	
	// 手动立即刷新DNS解析
	@GetMapping("/refresh/ip")
	@ResponseBody
	public String refreshIP(String queryIPType) throws CustomException {
		String modifyIP = null;
		if (StringUtils.isBlank(queryIPType)) {
			modifyIP = dynamicDomainService.DNSRecordModify(QueryIPType.IPCN.name());
		} else {
			QueryIPType type = QueryIPType.getType(StringUtils.upperCase(queryIPType));
			if (null == type) {
				String queryIPTypsList = ArrayUtil.join(QueryIPType.values(), ",");
				return String.format("未找到%s类型的IP解析网站,当前支持的查询IP解析网站：%s", queryIPType, queryIPTypsList);
			}
			dnsPodProperties.setQueryIPType(type.name());
			modifyIP = dynamicDomainService.DNSRecordModify(type.name());
		}
		return String.format("当前解析的IP地址为：%s", modifyIP);
	}
}
